package com.glsc.ngateway.opmanage.service.report.excel.listener.regulatory;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Cell;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.glsc.ngateway.common.base.domain.mysql.opmanage.report.fileupload.ImportTemplateRulesDefine;
import com.glsc.ngateway.common.base.domain.mysql.opmanage.report.regulatory.RegRptInterOtcA11;
import com.glsc.ngateway.common.base.repo.mysql.opmanage.report.regulatory.RegRptInterOtcA11Repo;
import com.glsc.ngateway.common.base.utils.ExcelDoubleConverter;
import com.glsc.ngateway.opmanage.service.report.excel.listener.TemplateCheckHandler;
import org.springframework.util.CollectionUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: qiyr
 * @date: 2022/6/27 13:38
 * @description: 场外债券债券交易杠杆数据导入监听器
 */
public class RegRptInterOtcA11Listener extends AnalysisEventListener<Map<Integer, String>> {

    private static final int BATCH_COUNT = 3000;
    List<RegRptInterOtcA11> list = new ArrayList<>();

    private List<ImportTemplateRulesDefine> rulesDefineList;
    private RegRptInterOtcA11Repo regRptInterOtcA11Repo;
    private BigInteger mainId;
    private String name;
    private String term;
    private String date;
    private String version = "V1";

    public RegRptInterOtcA11Listener() {
    }

    public RegRptInterOtcA11Listener(RegRptInterOtcA11Repo regRptInterOtcA11Repo, BigInteger mainId,
                                     List<ImportTemplateRulesDefine> rulesDefineList) {
        this.regRptInterOtcA11Repo = regRptInterOtcA11Repo;
        this.mainId = mainId;
        this.rulesDefineList = rulesDefineList;
    }

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        TemplateCheckHandler.check(context, rulesDefineList);
        ReadRowHolder readRowHolder = context.readRowHolder();
        Integer rowIndex = readRowHolder.getRowIndex();
        Map<Integer, Cell> dataMap = readRowHolder.getCellMap();

        if (rowIndex == 1) {
            name = data.get(1);
        } else if (rowIndex == 2) {
            term = data.get(1);
        } else if (rowIndex == 3) {
            date = data.get(1);
        } else if (rowIndex == 11) {
            if(data.get(10).equals("账面价值")){
                version = "V2";
            }
        }
        else if (rowIndex == 12) {
            if(version.equals("V1")){
                list.add(new RegRptInterOtcA11(mainId, name, term, date, rowIndex + 1, data.get(0), data.get(1),
                        ExcelDoubleConverter.cellToDouble(dataMap, 2),
                        ExcelDoubleConverter.cellToDouble(dataMap, 3),
                        ExcelDoubleConverter.cellToDouble(dataMap, 4),
                        ExcelDoubleConverter.cellToDouble(dataMap, 5),
                        ExcelDoubleConverter.cellToDouble(dataMap, 6),
                        ExcelDoubleConverter.cellToDouble(dataMap, 7),
                        ExcelDoubleConverter.cellToDouble(dataMap, 8),
                        null,null));
            }else{
                list.add(new RegRptInterOtcA11(mainId, name, term, date, rowIndex + 1, data.get(0), data.get(1),
                        ExcelDoubleConverter.cellToDouble(dataMap, 2),
                        ExcelDoubleConverter.cellToDouble(dataMap, 3),
                        ExcelDoubleConverter.cellToDouble(dataMap, 4),
                        ExcelDoubleConverter.cellToDouble(dataMap, 5),
                        ExcelDoubleConverter.cellToDouble(dataMap, 8),
                        ExcelDoubleConverter.cellToDouble(dataMap, 9),
                        ExcelDoubleConverter.cellToDouble(dataMap, 10),
                        ExcelDoubleConverter.cellToDouble(dataMap, 6),
                        ExcelDoubleConverter.cellToDouble(dataMap, 7)));
            }

        }

        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }

    private void saveData() {
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("导入失败，数据量为空，请检查文件格式与内容");
        }
        regRptInterOtcA11Repo.saveAll(list);
    }
}
